package cn.icanci.loopstack.bic.biz.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;

import cn.hutool.json.JSONUtil;
import cn.icanci.loopstack.bic.biz.adapter.GraphAdapter;
import cn.icanci.loopstack.bic.biz.service.SnapshotService;
import cn.icanci.loopstack.bic.biz.service.UnitViewSnapshotIndexService;
import cn.icanci.loopstack.bic.biz.service.UnitViewSnapshotService;
import cn.icanci.loopstack.bic.common.model.snapshot.UnitViewSnapshotIndexVO;
import cn.icanci.loopstack.bic.common.model.snapshot.UnitViewSnapshotVO;
import cn.icanci.loopstack.bic.graph.Graph;

/**
 * 快照服务
 *
 * @author icanci
 * @since 1.0 Created in 2023/09/21 00:09
 */
@Order(1)
@Service
public class SnapshotServiceImpl implements SnapshotService {
    @Resource
    private UnitViewSnapshotService      unitViewSnapshotService;
    @Resource
    private UnitViewSnapshotIndexService unitViewSnapshotIndexService;
    @Resource
    private GraphAdapter                 graphAdapter;

    @Override
    public void refresh(String systemUuid, String useCaseUuid) {
        long version = refreshSnapshotIndex(systemUuid, useCaseUuid);
        Graph graph = graphAdapter.graph(systemUuid, useCaseUuid);
        String snapshot = JSONUtil.toJsonStr(graph);
        UnitViewSnapshotVO unitViewSnapshot = new UnitViewSnapshotVO();
        unitViewSnapshot.setSystemUuid(systemUuid);
        unitViewSnapshot.setUseCaseUuid(useCaseUuid);
        unitViewSnapshot.setVersion(version);
        unitViewSnapshot.setSnapshot(snapshot);
        unitViewSnapshot.setName(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(version));
        unitViewSnapshotService.save(unitViewSnapshot);
    }

    @Override
    public List<UnitViewSnapshotVO> loadUseCaseSnapshot() {
        List<UnitViewSnapshotIndexVO> indexes = unitViewSnapshotIndexService.queryAll();
        List<UnitViewSnapshotVO> ret = Lists.newArrayList();
        if (CollectionUtils.isEmpty(indexes)) {
            return ret;
        }

        for (UnitViewSnapshotIndexVO index : indexes) {
            UnitViewSnapshotVO snapshot = unitViewSnapshotService.queryBySystemUuidAndUseCaseUuidAndVersion(index.getSystemUuid(), index.getUseCaseUuid(), index.getVersion());
            if (snapshot != null) {
                ret.add(snapshot);
            }
        }
        return ret;
    }

    @Override
    public UnitViewSnapshotVO queryBySystemUuidAndUseCaseUuid(String systemUuid, String useCaseUuid) {
        UnitViewSnapshotIndexVO index = unitViewSnapshotIndexService.queryBySystemUuidAndUseCaseUuid(systemUuid, useCaseUuid);
        if (index != null) {
            return unitViewSnapshotService.queryBySystemUuidAndUseCaseUuidAndVersion(systemUuid, useCaseUuid, index.getVersion());
        }
        return null;
    }

    /**
     * 刷新快照索引并返回版本
     *
     * @param systemUuid systemUuid
     * @param useCaseUuid useCaseUuid
     * @return 版本
     */
    private long refreshSnapshotIndex(String systemUuid, String useCaseUuid) {
        UnitViewSnapshotIndexVO index = unitViewSnapshotIndexService.queryBySystemUuidAndUseCaseUuid(systemUuid, useCaseUuid);
        long version = System.currentTimeMillis();
        if (index == null) {
            index = new UnitViewSnapshotIndexVO();
            index.setSystemUuid(systemUuid);
            index.setUseCaseUuid(useCaseUuid);
            index.setVersion(version);
        } else {
            index.setVersion(version);
        }
        unitViewSnapshotIndexService.save(index);
        return version;
    }

}
